package com.vanguard.controller;

import com.vanguard.commons.response.CommonReturnCode;
import com.vanguard.commons.response.HttpResult;
import com.vanguard.domain.User;
import com.vanguard.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: 用户前端控制器
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2019/02/13
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public HttpResult save(@RequestBody User user) {
        userService.save(user);
        return new HttpResult(CommonReturnCode.OK);
    }

    @GetMapping("/{id}")
    public HttpResult user(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        return new HttpResult(CommonReturnCode.OK, user);
    }

    @GetMapping("/list")
    public HttpResult list() {
        List userList = userService.findList();
        return new HttpResult(CommonReturnCode.OK, userList);
    }

    @DeleteMapping("/{id}")
    public HttpResult delete(@PathVariable("id") Long id) {
        userService.delete(id);
        return new HttpResult(CommonReturnCode.SUCCESS);
    }

}
